<table id="rolelistdg"></table>
<div id="rolelisttoolbar">
    <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-add" plain="true" onclick="newRole()">新建</a>
    <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-edit" plain="true" onclick="editRole()">编辑角色</a>
    <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-edit" plain="true" onclick="editPrivilege()">编辑权限</a>
    <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-remove" plain="true" onclick="destroyRole()">删除</a>
</div>
<div id="dlg-role" class="easyui-dialog" data-options="modal:true,closed: true, buttons:'#dlg-role-buttons'" style="width:400px;height:280px;padding:30px 0 0 40px;">
    <form id="role_fm" method="post" novalidate>
    	<table cellpadding="5">
            <tr>
                <td>角色名:</td>
                <td><input name="role_name" class="easyui-textbox" required="true" style="width:200px;"></td>
            </tr>
            <tr>
                <td>用户组:</td>
                <td><input name="role_groupname" class="easyui-textbox" required="true" style="width:200px;"></td>
            </tr>
            <tr>
                <td>排　序:</td>
                <td><input name="role_sort" class="easyui-numberbox" data-options="min:1,max:100,required:true" missingMessage="输入排序号，1-100以内" style="width:200px;"></td>
            </tr>
        </table>
    </form>
</div>
<div id="dlg-role-buttons">
    <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-ok" onclick="saveRole()">保存</a>
    <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-cancel" onclick="javascript:$('#dlg-role').dialog('close')">取消</a>
</div>

<div id="dlg-privilege" class="easyui-dialog" style="width:400px;height:450px;padding:20px" closed="true" buttons="#dlg-privilege-buttons">
	<ul id="privilegetree"></ul>
</div>
<div id="dlg-privilege-buttons">
    <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-ok" onclick="saveCheckedPrivilege()">保存</a>
    <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-cancel" onclick="javascript:$('#dlg-privilege').dialog('close')">取消</a>
</div>
<script type="text/javascript">
	var url;
	
	function set_rolelist_tree(){
		$('#rolelistdg').datagrid({
			url:'<?php echo base_url('systemmanage/role_data');?>?_='+GetRandomNum(),
			title:'角色列表',
			singleSelect:true,
			method:'post',
			rownumbers:true,
			fit:true,
			border:false,
			toolbar:'#rolelisttoolbar',
			columns:[[
				{field:'role_name',title:'角色名',width:160,align:'center'},
				{field:'role_groupname',title:'用户组',width:160,align:'center'},
				{field:'role_sort',title:'排序',width:160,align:'center'}
			]],
			onDblClickRow:function(index,data){
				editRole();
			}
		});	
	}
	
	//初始化角色列表
	set_rolelist_tree();
	
	//新建角色
	function newRole(){
		$('#dlg-role').dialog('open').dialog('setTitle','新建角色');
		$('#role_fm').form('clear');
		url = '<?php echo base_url('systemmanage/role_add')?>';
	}
	
	//编辑角色
	function editRole(){
		var row = $('#rolelistdg').datagrid('getSelected');
		if (row){
			$('#dlg-role').dialog('open').dialog('setTitle','编辑角色');
			$('#role_fm').form('load',row);
			url = '<?php echo base_url('systemmanage/role_edit')?>/'+row.id;
		}else{
			$.messager.alert('警告','编辑角色，请先指定角色!','warning');
		}
	}
	
	//编辑权限
	function editPrivilege(){
		var row = $('#rolelistdg').datagrid('getSelected');
		if (row){
			$('#privilegetree').tree({
				url:'<?php echo base_url('systemmanage/menu_data')?>/'+row.id+'?_='+GetRandomNum(),
				method:'post',
				animate:true,
				lines:false,
				checkbox:true,
				onLoadSuccess: function(node){
					$('#dlg-privilege').dialog('open').dialog('setTitle',' 为“'+row.role_name+'”组　编辑权限');
					//$('#privilegetree').tree({cascadeCheck:$(this).is(':checked')});
				},
				onLoadError: function(node){
					$.messager.alert('错误','权限列表加载错误!','error');
				}
			});
			
		}else{
			$.messager.alert('警告','编辑权限，请先指定角色!','warning');
		}
	}
	
	//保存角色
	function saveRole(){
		$('#role_fm').form('submit',{
			url: url,
			onSubmit: function(){
				return $(this).form('enableValidation').form('validate');
			},
			success: function(result){
				var result = eval('('+result+')');
				if (result.errorMsg){
					$.messager.alert('错误',result.errorMsg,'error');
				} else {
					$('#dlg-role').dialog('close');        // close the dialog
					//$('#rolelistdg').datagrid('reload');    // reload the user data
					set_rolelist_tree();
				}
			}
		});
	}
	
	//删除角色
	function destroyRole(){
		var row = $('#rolelistdg').datagrid('getSelected');
		if (row){
			$.messager.confirm('确认','你确定要删除这个角色吗?',function(r){
				if (r){
					$.post('<?php echo base_url('systemmanage/role_del');?>',{id:row.id},function(result){
						if (result.success){
							//$('#rolelistdg').datagrid('reload');    // reload the user data
							set_rolelist_tree();
						} else {
							$.messager.alert('错误',result.errorMsg,'error');
						}
					},'json');
				}
			});
		}else{
			$.messager.alert('警告','删除角色，请先指定角色!','warning');
		}
	}
	
	//保存权限
	function saveCheckedPrivilege(){
		var row = $('#rolelistdg').datagrid('getSelected');
		var nodes = $('#privilegetree').tree('getChecked',['checked','indeterminate']);//['checked','indeterminate']获取勾选和模糊勾选的值
		var s = '';
		for(var i=0; i<nodes.length; i++){
			if (s != '') s += ',';
			s += nodes[i].priv_id;
		}
		$.post('<?php echo base_url('systemmanage/role_privilege_edit');?>',{role_id:row.id,privilege_id:s},function(result){
			if (result.success){
				$('#dlg-privilege').dialog('close');        // close the dialog
				$.messager.alert('成功','修改成功!','info');
			} else {
				$.messager.alert('错误',result.errorMsg,'error');
			}
		},'json');
	}

</script>
